package com.ea.modular.controller.sys;


import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ea.common.annotation.LogAnnotation;
import com.ea.common.parent.controller.BaseController;

import io.swagger.annotations.ApiOperation;

import com.ea.modular.service.sys.FileService;
import com.ea.modular.model.sys.File;
import com.ea.modular.model.sys.LayuiFile;
import com.ea.modular.model.sys.LayuiFile.LayuiFileData;


/**
 * FileController控制器
 * @author jun
 *
 */
@Controller
@RequestMapping(value="/file")
public class FileController extends BaseController {

    @Autowired
    private FileService fileService;

    private String PREFIX = "/system/file/";
	/**
	 * 跳转到主页
	 * @return
	 */
	@ApiOperation(value = "首页")
	@RequestMapping(value = "",method=RequestMethod.GET)
	public String home() {		 
		return PREFIX + "file";
	}

	/**
	 * 列表
	 * @param File
	 * @return
	 */
	@LogAnnotation
	@ApiOperation(value = "列表")
	@RequestMapping(value = "/list",method=RequestMethod.POST)
	@ResponseBody
	public Object list(File file){
		return fileService.findPage(file);
	}
	
	/**
	 * 删除
	 * @return
	 */
	@LogAnnotation
	@ApiOperation(value = "删除")
	@RequestMapping(value = "/delete",method=RequestMethod.POST)
	@ResponseBody
	public int delete(String id) {
		return fileService.delete(id);
	}
	
	
  


	
	
	/**
	 * layui富文本文件自定义上传
	 * 
	 * @param file
	 * @param domain
	 * @return
	 * @throws IOException
	 */
	@LogAnnotation
	@PostMapping("/upload")
	@ApiOperation(value = "layui富文本文件自定义上传")
	@ResponseBody
	public LayuiFile uploadLayuiFile(MultipartFile file, String domain) throws IOException {
		File fileInfo = fileService.upload(file);

		LayuiFile layuiFile = new LayuiFile();
		layuiFile.setCode(0);
		LayuiFileData data = new LayuiFileData();
		layuiFile.setData(data);
		data.setSrc(domain + "/files" + fileInfo.getUrl());
		data.setTitle(file.getOriginalFilename());

		return layuiFile;
	}
}
